package cn.edu.szu.bigdata.util;

import com.qihoo.wzws.rzb.util.ip.IPRegion;
import net.ipip.ipdb.CityInfo;

public class IPUtil {
    public static IPRegion getIPRegionLocal(String ip) {
        CityInfo cityInfo = IPDataHandler.findGeography(ip);
        if (cityInfo != null) {
            IPRegion region = new IPRegion();
            region.setCity(cityInfo.getCityName());
            region.setCountry(cityInfo.getCountryName());
            region.setProvince(cityInfo.getRegionName());
            region.setIp(ip);
            return region;
        } else {
            return null;
        }
    }

    public static String getRegion(String ip) {
        String rv = "";
        IPRegion ipRegion = getIPRegionLocal(ip);
        if (ipRegion.getCountry().indexOf("中国") > -1) {
            if (ipRegion.getCity() != null && !ipRegion.getProvince().equals(ipRegion.getCity())) {
                rv = ipRegion.getProvince() + ipRegion.getCity();
            } else {
                rv = ipRegion.getProvince();
            }
        } else {
            rv = ipRegion.getCountry();
        }

        return rv;
    }
}
